﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DeviceInfoManage.BusinessLogicLayer;
public partial class MaintRecordUpdate : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["adminUserName"] == null)
            {
                Response.Redirect("login.aspx");
            }
            BindPerson();

            /*根据设备编号得到设备各个字段的信息并添入个控件中*/
            hidRecordId.Value = Request.QueryString["recordId"];
            maintRecordInfo rd = new maintRecordInfo();
            rd = rd.GetModel(Int32.Parse(hidRecordId.Value));
            this.DeviceName.Text = rd.deviceUser;
            this.DeviceDpt.SelectedValue = rd.departmentName;
            this.startTime.Value = rd.maintTime.ToString();
            this.endTime.Value = rd.ComTime.ToString();
            this.ddlErrType.Text = rd.errType;
            this.DeviceModel.Text = rd.deviceModel;
            this.DeviceSerialNumber.Text = rd.deviceSerialNumber;
            this.txtPhone.Text = rd.propertySerialNumber;
            if (ddlMaintPerson.Items.FindByValue(rd.maintPersonId.ToString()) != null)
            {
                this.ddlMaintPerson.SelectedValue = rd.maintPersonId.ToString();
            }
            else
            {
                ddlMaintPerson.Items.Insert(0, new ListItem("已离职", "0"));
                Btn_Update.Enabled = false;
            }
            txtErrorReason.Text = rd.errReason;
            this.DeviceMemo.Text = rd.errSolveWay;
            if (rd.IsSolve == "1") { this.cbisSolve.Checked = true; }
            else { this.cbisSolve.Checked = false; }

        }
    }
    private void BindPerson()
    {
        ddlMaintPerson.DataSource = (new MaintPerson()).GetMaintPersonName();
        ddlMaintPerson.DataValueField = "maintPersonId";
        ddlMaintPerson.DataTextField = "maintPersonName";
        ddlMaintPerson.DataBind();
        //ddlMaintPerson.Items.FindByText(Session["UserName"].ToString()).Selected = true;
    }
    protected void Btn_Update_Click(object sender, EventArgs e)
    {
        /*建立一个记录对象，然后传入各个字段的属性值，最后调用插入函数执行添加*/

        maintRecordInfo device = new maintRecordInfo();
        device.recordId = int.Parse(hidRecordId.Value);
        device.deviceUser = this.DeviceName.Text;
        device.departmentName = this.DeviceDpt.SelectedValue;
        device.maintTime = DateTime.Parse(this.startTime.Value);
        device.ComTime = Convert.ToDateTime(this.endTime.Value);
        device.errType = this.ddlErrType.SelectedValue;
        device.deviceModel = this.DeviceModel.Text;
        device.deviceSerialNumber = this.DeviceSerialNumber.Text;
        device.propertySerialNumber = this.txtPhone.Text;
        device.maintPersonId = int.Parse(this.ddlMaintPerson.SelectedValue);
        device.errReason = txtErrorReason.Text;
        device.errSolveWay = this.DeviceMemo.Text;
        device.State = "0";
        if (cbisSolve.Checked == true) { device.IsSolve = "1"; }
        else { device.IsSolve = "0"; }
        /*插入函数返回添加是否成功，如果失败，ErrMessage中保存了错误信息*/
        if (device.Update(device) != -1)
            Response.Write("<script>alert('设备维护信息更新成功!');</script>");//location.href='MaintRecordRegister.aspx';
        else
            Response.Write("<script>alert('更新失败!');</script>");
    }

    protected void Btn_Back_Click(object sender, EventArgs e)
    {
        Response.Redirect("MaintRecordList.aspx");
    }
}
